#include "Program.h"
#include "Hdr/VideoSystem.h"
#include "Hdr/Device.h"
#include "Hdr/Camera.h"
#include "Hdr/Input.h"
#include "Hdr/Md2Model.h"
#include "Hdr/Terrain.h"
#include "Hdr/geomdef.h"
#include "Hdr/Chunk.h"
#include <SDL/SDL_opengl.h>

Eternal::Program::Program()
{
	
}

Eternal::Program::~Program()
{
	
}

void Eternal::Program::Initialize()
{
	/* Allocate a new device and take references
		to all required sub-systems	*/
	Device = new Core::Device();
	InputManager = Device->GetInputManager();
	Camera = Device->GetCamera();
	VideoSys = Device->GetVideoSystem();
}

void Eternal::Program::Run()
{
	Initialize();
	
/*	Eternal::Scene::Terrain *Terrain = new Eternal::Scene::Terrain();
	Terrain->Load();*/
	
	Scene::Chunk *Chunk = new Scene::Chunk;
	//Chunk->gridLocX = 0;
	//Chunk->GridLocZ = 0;
	
	Chunk->GenerateVertices();
	
	Eternal::Scene::Md2Model *Md2 = new Eternal::Scene::Md2Model();
	
	Md2->Load("Data/Models/yoshi/yoshi.md2");
	
	VideoSys->Initialize();
	for(;;)
	{
		VideoSys->BeginScene();
	
		Camera->UseArrowsMove();
		Camera->UseMouseLook();
		
		Camera->BeginTransform();

		/*
			Camera relative rendering
			code goes here.
		*/
		
		Md2->Render();

		Camera->EndTransform();
		
		VideoSys->EndScene();
	}
}

void Eternal::Program::UpdateCamera()
{
	Camera->UseMouseLook();
	if ( InputManager->IsKeyPress(Eternal::KEY_LEFT) )
	{
		Camera->SetX(Camera->GetX() + 0.5f);
	}
	else if ( InputManager->IsKeyPress(Eternal::KEY_RIGHT) )
	{
		Camera->SetX(Camera->GetX() - 0.5f);
	}
	
	if ( InputManager->IsKeyPress(Eternal::KEY_UP) )
	{
		Camera->MoveForward(1.0f);
	}
	else if ( InputManager->IsKeyPress(Eternal::KEY_DOWN) )
	{
		Camera->MoveForward(-1.0f);
	}
}
